package com.moshang.blog.controller.back.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.entity.SysDict;
import com.moshang.blog.service.SysDictService;
import com.moshang.blog.core.utils.PageUtil;
import com.moshang.blog.core.utils.RestResponse;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @description: ${description}
 * @author: xieweiwei
 * @create: 2018-11-15 15:00
 **/
@Controller
@RequestMapping("/back/system/dict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;
    @GetMapping("list")
    @SystemLog("跳转系统字典页面")
    public String list(Model model){
        //查询去重的类型
        List<String> types=sysDictService.findDistinctType();
        model.addAttribute("types",types);
        return "back/system/dict/list";
    }

    @RequiresPermissions("sys:dict:list")
    @PostMapping("list")
    @ApiOperation(value = "/list", httpMethod = "POST", notes = "查询系统字典")
    @ResponseBody
    public PageUtil<SysDict> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                  @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                  ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        PageUtil<SysDict> pageUtil = new PageUtil<>();
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        if(!map.isEmpty()){
            String type = (String) map.get("type");
            if(StringUtils.isNotBlank(type)) {
                wrapper.eq("type", type);
            }
        }
        wrapper.orderByDesc("type","sort");
        IPage<SysDict> dataPage = sysDictService.page(new Page<>(page,limit),wrapper);
        pageUtil.setCount((int) dataPage.getTotal());
        pageUtil.setData(dataPage.getRecords());
        return pageUtil;
    }
    /**
     * 进入添加字典页面
     */
    @RequestMapping("addUI")
     public String addUI(Model model){
        //查询去重的类型
        List<String> types=sysDictService.findDistinctType();
        model.addAttribute("types",types);
         return "back/system/dict/add";
     }
    /**
     * 添加字典
     */
    @RequiresPermissions("sys:dict:add")
    @PostMapping("add")
    @SystemLog("新增系统字典")
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "新增系统字典")
    @ResponseBody
    public RestResponse add(SysDict sysDict){
        String type="insert";
        if(StringUtils.isBlank(sysDict.getType())){
            return RestResponse.failure("字典类型不能为空");
        }
        if(StringUtils.isBlank(sysDict.getValue())){
            return RestResponse.failure("字典标签对应的值不能为空");
        }
        sysDictService.saveOrUpdateDict(sysDict,type);
        return RestResponse.success();
    }
    @RequiresPermissions("sys:dict:edit")
    @RequestMapping("editUI")
    public String editUI(@RequestParam("id")Integer id,Model model){
        SysDict sysDict=sysDictService.getById(id);
        model.addAttribute("sysDict",sysDict);
        return "back/system/dict/edit";
    }

    /**
     * 修改字典
     * @param dict
     * @return
     */
    @RequiresPermissions("sys:dict:edit")
    @PostMapping("edit")
    @SystemLog("编辑系统字典")
    @ApiOperation(value = "/edit", httpMethod = "POST", notes = "编辑系统字典")
    @ResponseBody
    public RestResponse edit(SysDict dict){
        String type="update";
        if(dict.getId()==null || dict.getId() == 0){
            return RestResponse.failure("字典ID不能为空");
        }
        if(StringUtils.isBlank(dict.getType())){
            return RestResponse.failure("字典类型不能为空");
        }

        if(StringUtils.isBlank(dict.getValue())){
            return RestResponse.failure("字典标签对应的值不能为空");
        }
        if(dict.getSort() == null || dict.getSort() < 0){
            return RestResponse.failure("排序值不正确");
        }
        SysDict oldDict = sysDictService.getById(dict.getId());
        if(!oldDict.getType().equals(dict.getType())){
            return RestResponse.failure("字典类型不能修改");
        }
        sysDictService.saveOrUpdateDict(dict,type);
        return RestResponse.success();
    }
    /**
     * 删除系统字典
     */
    @RequiresPermissions("sys:dict:delete")
    @PostMapping("delete")
    @SystemLog("编辑系统字典")
    @ApiOperation(value = "/edit", httpMethod = "POST", notes = "编辑系统字典")
    @ResponseBody
    public RestResponse deleteById(@RequestParam("id") Integer id){
        sysDictService.removeById(id);
        return RestResponse.success();
    }
}
